package com.xiaoyu.im.util

import android.text.TextUtils
import com.tencent.mmkv.MMKV
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.utils.MMKVUtil.Companion.get
import com.xiaoyu.base.utils.MMKVUtil.Companion.getDefault

object ChatDraftUtils {
    private const val PREFIX = "chat_draft_"

    fun encodeChatDraft(fuid: String, message: String?) {
        if (TextUtils.isEmpty(message)) {
            return
        }
        val uid = UserData.getInstance().uid
        val saveKey = PREFIX + uid + "_" + fuid
        get(uid).encode(saveKey, message)
    }

    fun decodeChatDraft(fuid: String): String {
        val uid = UserData.getInstance().uid
        val saveKey = PREFIX + uid + "_" + fuid
        return if (!getDefault().contains(saveKey)) {
            ""
        } else MMKV.defaultMMKV().decodeString(saveKey, "")
    }

    fun removeChatDraft(fuid: String) {
        val uid = UserData.getInstance().uid
        val saveKey = PREFIX + uid + "_" + fuid
        getDefault().removeValueForKey(saveKey)
    }
}